
/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *	
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *	
 *	Copyright (c)2005-2006 Ryan Macnak <ambiguousprep@gmail.com>
 */

package com.servequake.truemusic;

import de.ueberdosis.mp3info.*;
import javazoom.jl.player.advanced.AdvancedPlayer;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// encapsulation of modified AdvancedPlayer to add frame control
public class GoodPlayer
{
	public static void main(String args[]) throws Exception
	{
		GoodPlayer p = new GoodPlayer(args[0]);
		
		int i=0;
		while(p.play(i))
		{
			i++;
			System.out.println(i);
		}
	}

	String file;
	AdvancedPlayer p;
	int current = 0;
	public int max = 0;

	GoodPlayer(String file) throws Exception
	{
		this(file,true);	
	}

	GoodPlayer(String file, boolean computeMax) throws Exception
	{
		this.file = file;
		p = new AdvancedPlayer( new FileInputStream( file ) );

		if(computeMax)
		{
			while(p.skipFrame())
			{
				max++;
				//if(max>9000) System.out.println(max);
			}

			p = new AdvancedPlayer( new FileInputStream( file ) );
		}
		else max = -1;
	}

	public void dispose()
	{
		if(p!=null)p.close();
		p=null;
	}

	public boolean play(int frame) throws Exception
	{
		if(p==null)
		{
			p = new AdvancedPlayer( new FileInputStream( file ) );
			current = 0;
		}

		if(frame<current)
		{
			p = new AdvancedPlayer( new FileInputStream( file ) );
			current = 0;
		}
		
		while(frame>current)
		{
			if( !p.skipFrame() ) return false;
			current++;
		}

		current++;
		return p.decodeFrame();
	}
}